package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.framework.utils.DateUtils;
import com.jservice.framework.utils.StringUtils;
import com.jservice.model.SmkAppUser;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2015 - 交易记录-钱包查询
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2015")
public class Upp2015 extends BaseService implements IService {

    private String SERVER_NAME = "upp.trade.details.query";


    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap = request.getData();
        String card_no = paramsStringFilter(paramMap.get("card_no"));
        String display_no = paramsStringFilter(paramMap.get("display_no"));
        String current_page_no = paramsStringFilter(paramMap.get("current_page_no"));
        String start_date = paramsStringFilter(paramMap.get("start_date"));
        String end_date = paramsStringFilter(paramMap.get("end_date"));

        if (StringUtils.isEmpty(display_no)) {
            display_no = "10";
        }
        if (StringUtils.isEmpty(current_page_no)) {
            current_page_no = "1";
        }
        if (StringUtils.isEmpty(start_date)) {
            start_date = DateUtils.format(DateUtils.getDayBeforeBegin(-30), "yyyyMMdd");
        }
        if (StringUtils.isEmpty(end_date)) {
            end_date = DateUtils.format(DateUtils.getDayEnd(), "yyyyMMdd");
        }

        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null) {
            return renderFAIL(MsgCode.Code_1007, response, header);
        }
        if (StringUtils.isEmpty(card_no)) {
            return renderFAIL(MsgCode.Code_1008, "卡号不能为空!", response, header);
        }
        Map data = new HashMap();
        data.put("card_no", card_no);
        data.put("account_type", "01");//01:电子钱包 02:联机账户 06:车    改账户
        data.put("record_type", "0");//记录类型。0：全部1：消费2：退货3：充值4：提现5：退现
        data.put("auth_type", "032");
        data.put("display_no", display_no);
        data.put("current_page_no", current_page_no);
        data.put("merchant_limit", "021401");
        data.put("start_date", start_date);
        data.put("end_date", end_date);

//        Map<String, Date> map  = DateUtils.getMonthDate(new Date());
        //默认查询近30天的
//        data.put("start_date", DateUtils.format(DateUtils.getDayBeforeBegin(-30), "yyyyMMdd"));
//        data.put("end_date", DateUtils.format(DateUtils.getDayEnd(), "yyyyMMdd"));

        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject, response, header);
        } else {
            return renderFAIL(MsgCode.Code_1009, "市民卡网关返回异常:" + jsonObject.getString("msg"), response, header);
        }

    }
}
